package com.mf.basic.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.mf.basic.rs.R;
import com.mf.basic.exceptions.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Luke on 2025/3/7 16:28.
 */
@ControllerAdvice
public class ConfigExceptionHandler {

    // 处理未登录异常
    @org.springframework.web.bind.annotation.ExceptionHandler(NotLoginException.class)
    @ResponseBody
    public R handlerNotLoginException(NotLoginException e) {
        return R.fail("未登录或登录已过期, 请检查", e.getMessage());
    }

    // 处理角色认证异常
    @org.springframework.web.bind.annotation.ExceptionHandler(NotRoleException.class)
    @ResponseBody
    public R handlerNotRoleException(NotRoleException e) {
        return R.fail("当前用户无该角色权限, 请检查", e.getMessage());
    }

    // 处理权限认证异常
    @org.springframework.web.bind.annotation.ExceptionHandler(NotPermissionException.class)
    @ResponseBody
    public R handlerNotPermissionException(NotPermissionException e) {
        return R.fail("当前用户无操作权限, 请检查", e.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(MyException.class)
    @ResponseBody
    public R handlerMyException(MyException e) {
        e.printStackTrace();
        return R.fail(e.getMessage(), null);
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    @ResponseBody
    public R handlerException(Exception e) {
        e.printStackTrace();
        return R.fail(e.getMessage(), e.getCause());
    }


}
